/*========================*\
# Cubes - the 3x3x3        #
# Charlieplex connection   #
\*========================*/

// Map a LED# to pinpair, the 1st to pull high, the 2nd to pull low
// order by left/right, Front/back, Top/bottom
const int PP[][2] = {
  {7,10}, {6,10}, {8,10}, {7, 9}, {6, 9}, {8, 9}, {7,11}, {6,11}, {8,11}, 
  {7, 6}, {9, 6}, {9,10}, {7, 8}, {11,9}, {9, 8}, {11,7}, {11,6}, {6, 8},  
  {6, 7}, {10,6},{10,11}, {8,10},{11,10}, {9,11}, {8, 7}, {8, 6}, {11,8},  
  };
//(Arduino note: All lookup tables could be placed in F-memory to save RAM)
const int PPlen=(sizeof(PP)/sizeof(PP[0])) ; // Number of LEDS (=27)

int b ;  // These two loop variables are declared here , due to some compiler bug?!?!
int n ;

void setup() {
  // pins to start in tristate (redundant as it is default initialized by core)
  for ( int n=6; n<=11; n++ ) pinMode(n,INPUT); 
}

// Here patterns are generated directly in the loop with code
// The loop only runs once for every complete sequence
// delay() is used as there is no other activity.

void loop() {

  // pattern 1 - turn on each LED singly
  for ( n=0; n<PPlen; n++) {
    PairOn(PP[n]) ;
    delay(250) ;
    PairOff(PP[n]) ;
    delay(150) ;
  }
  
  // Pattern 2 - turn all LEDs On/Off together, 3 times
  // This is NOT POSSIBLE. 
  // We have to turn them OnOff in quick succession

  // method one - each seperatly (each is on only on a 27th)
  for ( b=0; b<3; b++ ) {
    for ( n=0 ; n<=PPlen ; n++ ) {
      PairOn(PP[n]);
      delay(10); // 27*10 = 270ms for all to have turned on
      PairOff(PP[n]);
      // note: no offdelay
    }
    delay(250);  // they're already all off, so just show that
  }

  // method two - a pattern so that more then one LED are on,
  // which still needs to change quickly with the complementary patterns
  // As this overloads some Arduino pins the code is not written (yet)
  /* --- [TBW] --- */

  // Pattern 3 - Pan across axis
  // Again, NOT POSSIBLE, so we need to turn each set
  /* --- [TBW] --- */
}

void PairOn(const int LL[2]) {
  // change the pair of pins to turn on that LED
  pinMode(LL[0],OUTPUT); digitalWrite(LL[0],HIGH);
  pinMode(LL[1],OUTPUT); digitalWrite(LL[1],LOW) ;
}

void PairOff(const int LL[2]) {
  // change the pair of pins to tristate to turn LED off
  // (turn pullup resistor off, too)
  pinMode(LL[0],INPUT); digitalWrite(LL[0],LOW) ;
  pinMode(LL[1],INPUT); digitalWrite(LL[1],LOW) ;
}
